package 字节;


import java.util.*;

class Node{
    String name;
    Node parent;
    LinkedList<Node> child;
    Node(String name){
        this.name=name;
        this.parent=null;
        this.child=new LinkedList<>();
    }
    Node(String name,LinkedList<Node> chNode){
        this.name=name;
        this.child=new LinkedList<>();
        child.addAll(chNode);
    }
}
public class Main_1016_4 {
    static Map<String,Integer> res=new HashMap<>();
    static int get(Node root){
        if (root==null) return 0;
        int sum=0;
        for (Node c:root.child) {
            sum+=get(c);
        }
        res.put(root.name,sum);
        return sum;
    }
    public static void main(String[] args) {
        Scanner cin=new Scanner(System.in);
        int n=cin.nextInt();
        cin.nextLine();
        Node root=new Node(".");
        Map<String,Node> map=new HashMap<>();
        map.put(".",root);
        for (int i = 0; i < n; i++) {
            String[] line = cin.nextLine().split("/");
            if (line.length==0) continue;
            if (!line[0].equals(".")){
                continue;
            }
            for (int j = 1; j < line.length; j++) {
                String cur=line[j];
                Node curNode=map.getOrDefault(cur,new Node(cur));
                Node pre = map.get(line[j - 1]);
                pre.child.add(curNode);
                curNode.parent=pre;
            }
        }
        int q=cin.nextInt();
        cin.nextLine();
        for (int i = 0; i < q; i++) {
            String[] line = cin.nextLine().split(" ");
            Node form = map.get(line[1]);
            Node to = map.get(line[2]);
            to.child.add(form);
            form.parent=to;
        }
        get(root);
        System.out.println(res.size());
        for (Map.Entry<String,Integer> entry:res.entrySet()) {
            System.out.println(entry.getKey()+" "+entry.getValue());
        }
    }

}
